﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FrameWork.Lib;
using FrameWork.Lib.DataObjects;

namespace REVISIONMAX30.DataObjects
{
    public class SourceDirectory : DataObjectBase
    {
        public SourceDirectory()
        {
        }

        public SourceDirectory(string pDirectoryName)
            : base(true)
        {
            this.DirectoryName = pDirectoryName;
            this.SourcePatternList = new List<SourcePattern>();
            ///defaults
            this.SourcePatternList.Add(new SourcePattern("[0-9]+_[0-9]+_[a-zA-Z0-9]+_.*", "([0-9]+)_([0-9]+)_([a-zA-Z0-9]+)_(.*)", "$1\t$2\t$3\t$4"));
            this.SourcePatternList.Add(new SourcePattern("[0-9]+_[0-9]+_[a-zA-Z0-9]", "([0-9]+)_([0-9]+)_([a-zA-Z0-9]+)", "$1\t$2\t$3"));
        }


        public string DirectoryName { get; set; }
        public bool? ScanDirectoryRecusivly { get; set; }
        public List<SourcePattern> SourcePatternList { get; set; }

        public string[] GetFiles()
        {
            if (!DirectoryExists()) return null;
            return System.IO.Directory.GetFiles(this.DirectoryName, "*.sql", ScanDirectoryRecusivly.BoolValue() ? System.IO.SearchOption.AllDirectories : System.IO.SearchOption.TopDirectoryOnly);
        }

        public string[] ParseFileName(string pFileName)
        {
            foreach (var tSourcePattern in this.SourcePatternList)
            {
                var t = tSourcePattern.ParseFileName(pFileName);
                if (t != null)
                    return t;
            }
            return null;
        }

        public bool DirectoryExists()
        {
            return System.IO.Directory.Exists(this.DirectoryName);
        }

        public Result CopyFilesToLocal()
        {
            return new System.IO.DirectoryInfo(DirectoryName).CopyDirectoryWithFiles(ScanDirectoryRecusivly.BoolValue(), "*.sql", false);
        }
    }
}
